Creating a Background Service
By Alex Schendel
Introduction
Services are extremely useful in Android because they act similar to AsyncTasks because they run on a separate thread, but they have more freedom as to how they act with regard to the system, they are reusable, and they can continue to run even when the Activity has been stopped. Note: these services are still stopped when the app is killed.
Note 2: After playing around with Services again, I realize that you can keep a service alive in the background after the app is killed. (of course still not when you force stop the app)
Services
Services come in many “flavors”, but for our example, let’s make a service that will spam Toasts, even after the app is closed. (Because why not?)
1. Make an empty activity and then make a new class which extends Service (android.app). This will force you to implement the IBinder onBind() method.
2. Next we should implement the onCreate() and onStartCommand() methods.
3. In the onStartCommand method we can tell the Service what to do (spam with Toasts).
4. Now that we have Service that will complete these tasks, we need a Handler to handle the dirty work of completing these tasks. We will also need a thread to run it on, so we can use a HandlerThread for this and then initialize our Handler with the Looper of this thread.
5. Next we need to use our onStartCommand() method to return one of the possible integers. We should remove the while loop and instead have it return START_STICKY so that the service will attempt to recreate it if it is stopped. Also, we should have it send and receive messages via the Handler.
7. Now, we must start the service from the MainActivity. This requires making an intent and calling the startService() method.
8. You may have noticed that the Intent was never initialized. Let’s do that now. We will use the constructor Intent(Context, class). Which will be the activity and the Service Class.
9. We are so close! We need now need to declare the service in the manifest.
10. Finally, in order to get it to repeat itself, we do need to make the handler use its looper by calling the postDelayed() method. This involves making a runnable and telling the handler to continuously call the runnable after a delay.
11. Well try it out and when you get sick of it, go to the application manager and force stop the app ;) When you kill the app, there is a brief pause, but then the service recreates itself and it’s back to spamming you! Hope that was both informative and annoying!
How about running a task that should stay open indefinitely?
The best way to create such tasks are using services that call their startForeground() method. This allows them to be considered a service that the user is aware of (they are required to create a notification), and thus will not be killed in low memory situations. We can merely modify our current Service Project slightly to make it start in the foreground.
12. We need to create a NotificationManager, a notification, and then notify the user right before we call startForeground(id, notification).
In the notification bar appears a little android and there is a notification telling me that it is not spam, so it must not be the reason I have annoying toasts popping up every two seconds, right?
Well, I hope you had as much fun as I did making this tutorial ;)